/**
 * File  : Server.h
 * Time  : 2015-01-05 10:33
 * Author: KashingLee
 * Ref   : Null
 */
#ifndef PLCC_NET_SERVER_H_
#define PLCC_NET_SERVER_H_

#include "SocketAddr.h"
#include "Socket.h"
#include "../log/log.h"
#include "../event_queue/EventQueue.h"
#include <stdio.h>
#include <stdlib.h>
#include <fstream>

#define root_path "./program"
#define NETWORK "/etc/network/interfaces"

struct IPset{
  string address;
  string netmask;
  string network;
  string broadcast;
};

class Server {
 public:
  //Server();
  Server(sa_family_t, in_port_t, in_addr_t, int, EventQueue<LogType>*  );//family, port, addr, type
  Server(sa_family_t, in_port_t, const char *, int, EventQueue<LogType>*);//family, port, addr, type
  //~Server();

 public:
  void Set(sa_family_t, in_port_t, in_addr_t, int);
  void Set(sa_family_t, in_port_t, const char*, int);
  void Init();
  void Accept();
  int  SendMsg(char*, int, int);
  int  RecvMsg(char*, int, int);
  int  Decoder(char*, int, int);
  void Throwerror(int);
  void LogState(int ack,char* buffer);

 private:
	int SetTime();
	int SetIP();
	int DeleteFile();
 private:
  Socket      server_socket_;
  SocketAddr  server_addr_;
  SocketAddr  client_addr_;
  int         server_sockfd_;
  int         client_sockfd_;//init to -1
  int         size_;
  int         recv_program_;
  int         program_num_;
  int         set_;
  char        filename[30];
  string      time_;
  IPset      	ip_set_;
	EventQueue<LogType> *log_queue_;
};
#endif // PLCC_NET_SERVER_H_

